<?php


namespace app\api\controller;


use app\common\controller\Api;
use think\Hook;

class Message  extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Message;
    }

    public function list()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');

        $list =  $this->model
            ->with(['user'])->where(['user_id' => $this->auth->id])->order('is_read asc,id desc')->paginate($limit,false,['page' => $page]);


        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }

    public function readcount()
    {
        $count =  $this->model->where(['is_read' => 0, 'user_id' => $this->auth->id])->count('id');
        $this->success('返回成功',[
            'count' => $count
        ]);
    }

    public function setread($id = null)
    {
        $info = $this->model->get($id);
        $info->is_read = 1;
        $ret = $info->save();
        if($ret) {
            $this->success('操作成功');
        }else {
            $this->error('操作失败');
        }
    }
}